/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package miage.sessions.persistant;

import java.rmi.RemoteException;
import java.util.List;
import javax.ejb.FinderException;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import miage.beans.MagasinPresentation;
import miage.entities.Magasin;
import miage.entities.Produit;

/**
 *
 * @author Pepito
 */
@Stateless
public class AccountProductStoreFacade implements AccountProductStoreFacadeLocal {

    @PersistenceContext
    private EntityManager em;
    
    @Override
    public List<Magasin> getAllMagasins()throws FinderException, RemoteException {
        Query query = em.createNamedQuery("Magasin.findAll");
        List<Magasin> lesMagasins = query.getResultList();

        for(Magasin cust:lesMagasins ) {
            System.out.println(cust.toString());
        }    
        return lesMagasins;
    }
    @Override
    public Magasin getMagasin(String reference_magasin)throws FinderException, RemoteException {
        return (Magasin) em.createNamedQuery("findByReferenceStore").setParameter("referenceStore", reference_magasin).getSingleResult();
    }
    @Override
    public void addMagasin(MagasinPresentation magasin)throws FinderException, RemoteException {
        Magasin m = new Magasin();
        m.setIbanStore(magasin.getIban_store());
        m.setNomStore(magasin.getNom_store());
        m.setReferenceStore(magasin.getReference_store());
        m.setSolde(magasin.getSolde());
        m.setAdresseStore(magasin.getAdresseStore());
        m.setCodepostalStore(magasin.getCodepostalStore());
        m.setVilleStore(magasin.getVilleStore());
        em.persist(m);
    }
    @Override
    public void updateMagasin(MagasinPresentation magasin)throws FinderException, RemoteException {
        Magasin m = new Magasin();
        m.setNomStore(magasin.getNom_store());
        m.setReferenceStore(magasin.getReference_store());
        m.setSolde(magasin.getSolde());
        m.setAdresseStore(magasin.getAdresseStore());
        m.setCodepostalStore(magasin.getCodepostalStore());
        m.setVilleStore(magasin.getVilleStore());
        try{
            em.getTransaction().begin();
            em.merge(m);
            em.getTransaction().commit();
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
        finally{
            em.close();
        }
    }
    @Override
    public void deleteMagasin(String magasin)throws FinderException, RemoteException {
        Magasin m = this.getMagasin(magasin);
        if(m != null){
            try{
                em.getTransaction().begin();
                //suppression des produits associés au magasin
                ProductFacadeLocal pdf=null;
                List<Produit> produits = pdf.getAllProduitByMagasin(magasin);
                for(Produit cust:produits){
                    em.remove(cust);
                    em.flush();
                }
                em.remove(m);
                em.getTransaction().commit();
            }catch(Exception e){
                System.out.println(e.getMessage());
            }
            finally{
                em.close();
            }
        }
    }
}
